<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="商户名称" prop="tenantId">
        <el-select v-model="queryParams.tenantId" @change="chooseTenant" filterable clearable placeholder="请输入商户名称"
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name"
                     :value="parseInt(tenant.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="员工名称" prop="shopUserName">
        <el-input v-model="queryParams.shopUserName" placeholder="请输入员工名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="退单号" prop="shopRefundNo">
        <el-input v-model="queryParams.shopRefundNo" placeholder="请输入退单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商户单订单号" prop="merchantOrderNo">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户单订单号" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退单类型" prop="shopType">
        <el-select v-model="queryParams.shopType" placeholder="请选择退单类型" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_shop_type)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退单状态" prop="merchantStatus">
        <el-select v-model="queryParams.merchantStatus" placeholder="请选择退单状态" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_refund_merchant_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_pay_status_refund)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker v-model="queryParams.refundTime" style="width: 320px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="退款发货时间" prop="receivingTime" label-width="100px">
        <el-date-picker v-model="queryParams.receivingTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
      <!--                   v-hasPermi="['pekingfish:app-merchant-order-refund:create']">新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:app-merchant-order-refund:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" border :max-height="tableHeight" :data="list">
      <el-table-column label="操作" align="center" width="100" fixed="left" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.merchantStatusRefund === 1&&scope.row.shopType===5" size="mini" type="text"
                     icon="el-icon-edit"
                     @click="handleRefundGoods(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:app-merchant-order-refund:refundGoods']"
          >发货
          </el-button>
          <el-button v-if="scope.row.merchantStatusRefund === 0" size="mini" type="text" icon="el-icon-edit"
                     @click="handleConfirmRefund(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:app-merchant-order-refund:confirmRefund']"
          >确认退款
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:app-merchant-order-refund:detail']"
          >订单详细
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="自增主键" v-if="false" align="center" prop="id"/>
      <el-table-column label="退单号" align="center" prop="shopRefundNo"/>
      <el-table-column label="订单号" align="center" prop="shopOrderNo"/>
      <el-table-column label="退单类型" align="center" prop="shopType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_shop_type" :value="scope.row.shopType"/>
        </template>
      </el-table-column>
      <el-table-column label="商户单订单号" align="center" prop="merchantOrderNo" width="150" fixed="left"/>
      <el-table-column label="员工等级" align="center" prop="grade">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="员工名称" align="center" prop="shopUserName"/>
      <el-table-column label="商户类型" align="center" prop="accountType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.accountType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="merchantStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_merchant_status" :value="scope.row.merchantStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="退单状态" align="center" prop="merchantStatusRefund">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_refund_merchant_status" :value="scope.row.merchantStatusRefund"/>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_pay_status_refund" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="手机品牌" align="center" prop="brand"/>
      <el-table-column label="手机型号" align="center" prop="model"/>
      <el-table-column label="型号类型" align="center" prop="modelType"/>
      <el-table-column label="平台报价" align="center" prop="offerPrice"/>
      <el-table-column label="回收价" align="center" prop="recovery"/>
      <el-table-column label="扣款金额" align="center" prop="refundPrice"/>
      <el-table-column label="实付金额" align="center" prop="amountPaidPrice"/>
      <el-table-column label="门店名称" align="center" prop="shopName"/>
      <el-table-column label="门店店长" align="center" prop="shopNameUserName"/>
      <el-table-column label="分公司名称" align="center" prop="subTenantName"/>
      <el-table-column label="商户名称" align="center" prop="tenantName"/>
      <el-table-column label="订单时间" align="center" prop="merchantOrderTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.merchantOrderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户确认时间" align="center" prop="verifyTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.verifyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款发货时间" align="center" prop="receivingTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.receivingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货物流单号" align="center" prop="trackingNumber"/>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="openDetail" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="30">
            <el-form-item label="品牌：">{{ form.brand }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="手机型号：">{{ form.model }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="型号类型：">{{ form.modelType }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IMEI：">{{ form.imei }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数：">{{ form.memory }}，{{ form.colour }}，{{ form.buy }}，{{
                form.network
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机况：">{{ form.subrackDesc }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片：">
              <imageUpload v-model="form.reportImg"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.buckleImg!=null">
            <el-form-item label="凭证图片：">
              <imageUpload v-model="form.buckleImg"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.buckleVideo!=null">
            <el-form-item label="凭证视频：">
              <VideoUpload v-model="form.buckleVideo"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDetail = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报价单ID" prop="reportId">
          <el-input v-model="form.reportId" placeholder="请输入报价单ID"/>
        </el-form-item>
        <el-form-item label="渠道用户" prop="shopUserId">
          <el-input v-model="form.shopUserId" placeholder="请输入渠道用户"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="shopUserPhone">
          <el-input v-model="form.shopUserPhone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="退单类型 10部分退款 5退款退货" prop="shopType">
          <el-select v-model="form.shopType" placeholder="请选择退单类型 10部分退款 5退款退货">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="退单号" prop="shopRefundNo">
          <el-input v-model="form.shopRefundNo" placeholder="请输入退单号"/>
        </el-form-item>
        <el-form-item label="订单号" prop="shopOrderNo">
          <el-input v-model="form.shopOrderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="商户单订单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" placeholder="请输入商户单订单号"/>
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-radio-group v-model="form.payStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退单状态" prop="merchantStatus">
          <el-radio-group v-model="form.merchantStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="回收价" prop="recovery">
          <el-input v-model="form.recovery" placeholder="请输入回收价"/>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额"/>
        </el-form-item>
        <el-form-item label="订单时间" prop="merchantOrderTime">
          <el-date-picker clearable v-model="form.merchantOrderTime" type="date" value-format="timestamp"
                          placeholder="选择订单时间"
          />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable v-model="form.refundTime" type="date" value-format="timestamp"
                          placeholder="选择退款时间"
          />
        </el-form-item>
        <el-form-item label="退款发货时间" prop="receivingTime">
          <el-date-picker clearable v-model="form.receivingTime" type="date" value-format="timestamp"
                          placeholder="选择退款发货时间"
          />
        </el-form-item>
        <el-form-item label="快递名称" prop="trackingName">
          <el-input v-model="form.trackingName" placeholder="请输入快递名称"/>
        </el-form-item>
        <el-form-item label="京东订单号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入京东订单号"/>
        </el-form-item>
        <el-form-item label="物流单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入物流单号"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkStatus">
          <el-radio-group v-model="form.checkStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="checkRemark">
          <el-input v-model="form.checkRemark" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createAppMerchantOrderRefund,
  updateAppMerchantOrderRefund,
  deleteAppMerchantOrderRefund,
  getAppMerchantOrderRefund,
  getAppMerchantOrderRefundPage,
  exportAppMerchantOrderRefundExcel,
  confirmRefund,
  refundGoods
} from '@/api/pekingfish/appMerchantOrderRefund'
import { getBranchCompanyByTenantId } from '@/api/merchantManagement/branchCompany'
import { getBusinessList } from '@/api/merchantManagement/business'
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import VideoUpload from '@/components/VideoUpload'
import Editor from '@/components/Editor'
import { consent } from '@/api/pekingfish/appReportOrder'

export default {
  name: 'AppMerchantOrderRefund',
  components: {
    VideoUpload, Editor,
    ImageUpload, ImagePreview
  },
  data() {
    return {
      tableHeight: document.documentElement.scrollHeight - 245 + 'px',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户单退单列表
      list: [],
      tenants: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        reportId: null,
        shopUserId: null,
        shopUserPhone: null,
        shopType: null,
        shopRefundNo: null,
        shopOrderNo: null,
        merchantOrderNo: null,
        payStatus: null,
        merchantStatus: null,
        recovery: null,
        refundPrice: null,
        createTime: [],
        merchantOrderTime: [],
        refundTime: [],
        receivingTime: [],
        trackingName: null,
        orderCode: null,
        trackingNumber: null,
        checkStatus: null,
        checkRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantOrderNo: [{ required: true, message: '商户单订单号不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    // 商户名称
    getBusinessList().then(response => {
      this.tenants = response.data.list
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      this.queryParams.refundType = 'merchant'
      getAppMerchantOrderRefundPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    chooseTenant(tenantId) {
      console.log(tenantId + '获取的id')
      // 分公司名称
      getBranchCompanyByTenantId(tenantId).then(response => {
        this.subTenants = response.data
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        reportId: undefined,
        shopUserId: undefined,
        shopUserPhone: undefined,
        shopType: undefined,
        shopRefundNo: undefined,
        shopOrderNo: undefined,
        merchantOrderNo: undefined,
        payStatus: undefined,
        merchantStatus: undefined,
        recovery: undefined,
        refundPrice: undefined,
        merchantOrderTime: undefined,
        refundTime: undefined,
        receivingTime: undefined,
        trackingName: undefined,
        orderCode: undefined,
        trackingNumber: undefined,
        checkStatus: undefined,
        checkRemark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true
      this.form = row
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商户单退单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getAppMerchantOrderRefund(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商户单退单'
      })
    },

    handleConfirmRefund(row) {
      const merchantOrderNo = row.merchantOrderNo
      this.$modal.confirm('是否确认同意商户单订单号为"' + merchantOrderNo + '"的数据退款?').then(function() {
        return confirmRefund(row.id, row.merchantOrderNo, row.refundPrice)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('退款成功')
      }).catch(() => {
      })
    },

    /** 发货按钮操作 */
    handleRefundGoods(row) {
      console.log(row.shopRefundNo)
      this.$prompt('请输入"' + row.shopRefundNo + '"的物流单号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        refundGoods(row.id, value).then(response => {
          this.getList()
          this.$modal.msgSuccess('发货成功')
        })
      }).catch(() => {
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          updateAppMerchantOrderRefund(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createAppMerchantOrderRefund(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除商户单退单编号为"' + id + '"的数据项?').then(function() {
        return deleteAppMerchantOrderRefund(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有商户单退单数据项?').then(() => {
        this.exportLoading = true
        return exportAppMerchantOrderRefundExcel(params)
      }).then(response => {
        this.$download.excel(response, '商户单退单.xls')
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
